﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.DataPartLib
{
    /// <summary>
    /// Interaction logic for ConditionImage.xaml
    /// </summary>
    public partial class ConditionImage : ConditionControl
    {
        private Metadata.Part _templateMetadata;
        private UI.Part _templatePart;

        public ImageSourceFile ImageSource
        {
            get
            {
                return (ImageSourceFile)GetValue(ImageSourceProperty);
            }
            set
            {
                SetValue(ImageSourceProperty, value);
            }
        }

        public static DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource",
            typeof(ImageSourceFile),
            typeof(ConditionImage),
            new PropertyMetadata(null, OnImageSourcePropertyChangedCallback));

        public ConditionImage()
        {
            InitializeComponent();
        }

        public override void SetProperty(string propertyName, object value)
        {
            if (propertyName == "ImageSource")
            {
                if (value != null)
                {
                    SetValue(ImageSourceProperty, value);
                }
            }
            else if (propertyName == "Template")
            {
                if (value == null)
                {
                    return;
                }
                _templateMetadata = ((CPPEI.Coolzon.Metadata.Template)value).Content;
            }
            else
            {
                base.SetProperty(propertyName, value);
            }
        }

        private static void OnImageSourcePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ConditionImage control = d as ConditionImage;
            control.ctlImage.Source = control.ImageSource.ImageSource;
            control.ctlImage.Visibility = Visibility.Visible;
        }

        protected override void OnDisplay()
        {
            if (_templateMetadata != null)
            {
                TemplateInstance templateInstance = TemplateInstanceCache.Reference(this._part, _templateMetadata);
                if (templateInstance != null)
                {
                    _templatePart = templateInstance.Instance;
                    if (_templatePart is DataItemPart)
                    {
                        (_templatePart as DataItemPart).DataResult = _dataResult;
                        (_templatePart as DataItemPart).XDataSeriesId = _dataSeries.Id;
                        (_templatePart as DataItemPart).XDataItemId = _dataItem.Id;
                        (_templatePart as DataItemPart).Active(this._part.PartContext);
                        _ctlLayout.Children.Add(_templatePart);
                    }
                    else if(_templatePart is DataPart)
                    {
                        (_templatePart as DataPart).DataResult = _dataResult;
                        (_templatePart as DataPart).Active(this._part.PartContext);
                        _ctlLayout.Children.Add(_templatePart);
                    }
                }
            }
        }

        protected override void OnDeactive()
        {
            if (_templatePart != null)
            {
                _templatePart.Deactive();
                _ctlLayout.Children.Remove(_templatePart);
                TemplateInstanceCache.Recovery(_templateMetadata, _templatePart);
            }
        }
    }
}
